﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public class ModelAmmoDrawer : AmmoDrawerBase
    {
        public ModelAmmoDrawer(Game game, Type supportableAmmoType)
            : base(game, supportableAmmoType)
        {
        }

        #region Override
         
        public override void DrawEffects(GameTime gameTime, AmmoBase ammo)
        {
            Vector2 rotation = ammo.ComputeObjectRotation();
            var world = Matrix.CreateRotationX(rotation.X)*Matrix.CreateRotationY(rotation.Y)*
                        Matrix.CreateTranslation(ammo.CurrentPosition);

            IAmmoModel modelAmmo = (IAmmoModel)ammo;

            Matrix[] transforms = new Matrix[modelAmmo.Model.Bones.Count];
            modelAmmo.Model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in modelAmmo.Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World=transforms[mesh.ParentBone.Index]*world;
                    effect.View = World.ViewMatrix;
                    effect.Projection=World.ProjMatrix;
                }
                mesh.Draw();
            }
        }

        #endregion

        #region Events

        #endregion

        #region Private methods      

        #endregion
    }
}
